<?php

namespace mpend\modules\team\models;

use Yii;
use common\helpers\ArrayHelper;
use common\models\workbook\WorkbookRecord;
use common\logics\WorkbookLogic;
use common\models\workbook\WorkbookMember;

/**
 * 班组记工本管理模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class TeamBookMemberDateSearch extends WorkbookMember {

    /**
     * @inheritdoc
     */
    public function buildQuery($alias = 't') {
        $query = static::find();
        if ($alias) {
            $query->alias($alias);
            $alias .= '.';
        }
        $query->active(static::STATUS_ACTIVE);
        $query->joinWith([
            'workbook' => function($q) {
                return $q->alias('workbook')->active(WorkbookLogic::STATUS_ACTIVE);
            }
        ]);
        $query->andWhere([
            $alias . 'workbook_id' => $this->workbook_id,
            'workbook.type' => WorkbookLogic::TYPE_TEAM,
        ]);

        $query->andFilterWhere([
            $alias . 'user_id' => $this->user_id,
        ]);

        $query->orderBy([
            $alias . 'role_id' => SORT_DESC,
            $alias . 'created_at' => SORT_ASC,
        ]);
        $query->debug();
        return $query;
    }

    /**
     * @inheritdoc
     */
    public function getMarks() {
        $date = $this->getExtraAttribute('date');
        $query = $this->getBooks([
            'date' => $date
        ]);
        $query->groupBy(['type']);
        $query->select([
            'type',
            'sum(amount) amount',
            'sum(workload) workload',
            'count(*) nums',
        ]);
        $data = $query->debug()->all();
        if (empty($data)) {
            return [];
        }
        $types = [
            WorkbookRecord::TYPE_WORK => '点工',
            WorkbookRecord::TYPE_LOAN => '借支',
            WorkbookRecord::TYPE_BALANCE => '结算',
        ];
        $res = [];
        foreach ($data as $item) {
            $text = ArrayHelper::getValue($types, $item->type, '--');
            $res[$item->type] = [
                'type' => $item->type,
                'text' => $text,
                'amount' => (float) $item->amount,
                'workloads' => (float) $item->workload,
                'nums' => (int) $item->nums,
            ];
        }
        foreach ($types as $type => $text) {
            if (isset($res[$type])) {
                continue;
            }
            $res[$type] = [
                'type' => $type,
                'text' => $text,
                'amount' => 0,
                'workloads' => 0,
                'nums' => 0,
            ];
        }
        sort($res);
        return array_values($res);
    }

    /**
     * 获取成员记工薄记录
     */
    public function getMarked($condition = []) {
        $date = $this->getExtraAttribute('date');
        $query = $this->getBooks([
            'date' => $date,
            'type' => WorkbookRecord::TYPE_WORK,
        ]);
        return $query->exists();
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'realname',
            'avatar',
            'user_id',
            'role_id',
            'abbrs',
            'marked',
            'records' => 'marks',
            'wage',
        ];
    }

}
